package example.java.lang.reflect.InvocationHandler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class CalculatorValidationHandler implements InvocationHandler {
	private Object target;
	
	public CalculatorValidationHandler(Object target) {
		super();
		this.target = target;
	}
	
	public static Object createProxy(Object target){
		return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new CalculatorValidationHandler(target));
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		for(Object arg: args){
			validate((Double)arg);
		}
		
		Object result = method.invoke(target, args);
		return result;
	}

	private void validate(Double arg) {
		if(arg < 0) 
			throw new IllegalArgumentException("Positive numbers only");
		
	}

}
